package org.gruposp2p.aularest.model;

import java.io.Serializable;
import java.math.BigDecimal;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.PostLoad;
import javax.persistence.PrePersist;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import org.gruposp2p.aularest.server.LinkConstants;

/**
 *
 * @author jj
 */
@XmlType
@XmlRootElement(name = "score")
@XmlAccessorType(XmlAccessType.NONE)
@Entity
@Table(name = "SCORE", catalog = "database", schema = "PUBLIC")
@NamedQueries({@NamedQuery(name = "Score.findAll", query = "SELECT s FROM Score s"), @NamedQuery(name = "Score.findById", query = "SELECT s FROM Score s WHERE s.id = :id"), @NamedQuery(name = "Score.findByValue", query = "SELECT s FROM Score s WHERE s.value = :value")})
public class Score implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "ID", nullable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    @Basic(optional = false)
    @Column(name = "VALUE", nullable = false, precision = 10, scale = 2)
    @XmlElement(name="value")
    private BigDecimal value;
    @JoinColumn(name = "ITEMCALIFICABLE_ID", referencedColumnName = "ID", nullable = false)
    @ManyToOne(optional = false)
    private Itemcalificable itemcalificableId;
    @JoinColumn(name = "STUDENT_ID", referencedColumnName = "ID", nullable = false)
    @ManyToOne(optional = false)
    private Student studentId;

    @Transient
    @XmlElement(name="scoreLink")
    private Link link;
    public Link getLink() {
        return link;
    }

    public void setLink(Link link) {
        this.link = link;
    }
    
    @Transient
    @XmlElement(name="itemcalificableLink")
    private Link itemcalificableLink;
    public Link getItemcalificableLink() {
        return itemcalificableLink;
    }

    @Transient
    @XmlElement(name="studentLink")
    private Link studentLink;
    public Link getStudentLink() {
        return studentLink;
    }

    public Score() {
    }

    public Score(Integer id) {
        this.id = id;
    }

    public Score(Integer id, BigDecimal value) {
        this.id = id;
        this.value = value;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public BigDecimal getValue() {
        return value;
    }

    public void setValue(BigDecimal value) {
        this.value = value;
    }

    public Itemcalificable getItemcalificableId() {
        return itemcalificableId;
    }

    public void setItemcalificableId(Itemcalificable itemcalificableId) {
        this.itemcalificableId = itemcalificableId;
    }

    public Student getStudentId() {
        return studentId;
    }

    public void setStudentId(Student studentId) {
        this.studentId = studentId;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Score)) {
            return false;
        }
        Score other = (Score) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "org.gruposp2p.restletjpa.model.Score[id=" + id + "]";
    }
    @PostLoad
    public void postProcess() {
        link = new Link();
        link.setHref(LinkConstants.getScoreLink(id.toString()));
        link.setId(id.toString());
        link.setName(value.toString());

        itemcalificableLink = itemcalificableId.getLink();

        studentLink = studentId.getLink();
    }

}
